package br.com.puertorico.model.entity.audit;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.envers.RevisionNumber;
import org.hibernate.envers.RevisionTimestamp;
import org.hibernate.envers.RevisionType;

/**
 * Cont&eacute;m os elementos fundamentais de todas as entidades do sistema.
 *
 * @author daniel.camargo
 * @version $Id$
 * @since 29/08/2009
 */
@MappedSuperclass
public abstract class BaseRevision implements Serializable {

	private static final long serialVersionUID = 7685980228797389535L;

	@Id
	@RevisionNumber
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "seq_revisao")
	private Long id;

	@RevisionTimestamp
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "data_criacao", nullable = false)
	private Date dataCriacao;

	@Column(name = "usuario_ultima_alteracao", nullable = false)
	private String usuarioUltimaAlteracao;

	@Transient
	private RevisionType tipoRevisao;

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean equals(Object entityCandidate) {
		if (entityCandidate == null) {
			return false;
		}
		if (entityCandidate instanceof BaseRevision) {
			BaseRevision canditado = (BaseRevision) entityCandidate;
			// testamos pelo id real
			if (getId() != null && canditado.getId() != null) {
				return getId().equals(canditado.getId());
			}
		}
		return false;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public int hashCode() {
		return getId().hashCode();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String toString() {
		String idStr = getId().toString();
		return new StringBuffer(idStr).append("@")
				.append(getClass().getSimpleName()).toString();
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Date getDataCriacao() {
		return dataCriacao;
	}

	public void setDataCriacao(Date dataCriacao) {
		this.dataCriacao = dataCriacao;
	}

	public String getUsuarioUltimaAlteracao() {
		return usuarioUltimaAlteracao;
	}

	public void setUsuarioUltimaAlteracao(String usuarioUltimaAlteracao) {
		this.usuarioUltimaAlteracao = usuarioUltimaAlteracao;
	}

	public RevisionType getTipoRevisao() {
		return tipoRevisao;
	}

	public void setTipoRevisao(RevisionType tipoRevisao) {
		this.tipoRevisao = tipoRevisao;
	}

}
